<?php
class Setting extends Admin_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->model(array("Msetting"));
	}
	
	public function index(){
		$this->breadcrumb->append_crumb(trans('breadcrumb_home'),admincp_url());
		$this->breadcrumb->append_crumb(trans('breadcrumb_setting'),base_url().'admincp/setting');
		$data['template']="setting/index";
		$data['title_page']='<i class="icon20 i-cogs"></i> '.trans('title_setting');
		$data['info'] = $this->Msetting->get_data();
		$path_upload = 'assets/filemanager/logo/';
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$allowed_ext = array('jpg','jpeg','png','gif');
			$dataInsertImage = array(
				'setting_logo' => $data['info']['setting_logo']
			);
			if(array_key_exists('setting_logo',$_FILES) && $_FILES['setting_logo']['error'] == 0 ){
            
	            $pic = $_FILES['setting_logo'];
	
	            if(!in_array(get_extension($pic['name']),$allowed_ext)){
	                exit_status('Only '.implode(',',$allowed_ext).' files are allowed!');
	            }            
	            
	            // Move the uploaded file from the temporary 
	            // directory to the uploads folder:

	            if(move_uploaded_file($pic['tmp_name'], './'.$path_upload.$pic['name'])){

					$dataInsertImage = array(
						'setting_logo' => $path_upload.$pic['name']
					);
	            }
	        }

	        if(isset($_POST['setting_public'])){
	        	$public = 'on';
	        }else{
	        	$public = 'off';
	        }

			$data = array(
				'setting_title' => $this->input->post('setting_title'),
				'setting_keywords' => $this->input->post('setting_keywords'),
				'setting_description' => $this->input->post('setting_description'),
				'setting_public' => $public,
				// 'setting_slogan' => $this->input->post('setting_slogan'),
				'setting_hotline' => $this->input->post('setting_hotline'),
				'setting_footer' => $this->input->post('setting_footer'),
			);

			$dataFinal = array_merge($data, $dataInsertImage);

			if($this->Msetting->count_row() == 0){		
				$this->Msetting->insert($dataFinal);
			}else{
				$this->Msetting->update($dataFinal);
			}
			$fp = fopen(APPPATH.'../assets/json/setting.json', 'w');
			fwrite($fp, json_encode($dataFinal));
			fclose($fp);
			$this->session->set_flashdata(array("success"=>trans('message_save_successfully')));
			redirect(base_url().'admincp');
		}
		$this->load->view("admincp/template",$data);
	}

	public function clearCache(){
		if($this->input->is_ajax_request()){
			$from = microtime(true);

			header ("Expires: ".gmdate("D, d M Y H:i:s", time())." GMT");  
			header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");  
			header ("Cache-Control: no-cache, must-revalidate");  
			header ("Pragma: no-cache");

			$this->cache->delete_all();

			$total = microtime(true) - $from;
            $result['html'] = '<div style="width:100%;">
									<span style="float:left"><strong><i class="icon24 i-checkmark-circle"></i> Clear Successful:</strong></span>
									<span class="badge" style="float:right; padding:5px">'.round($total,2).'s</span>
							   </div>';
            echo json_encode($result);
		}
	}

}